package com.example.address.controller;

import com.example.address.bean.Address;
import com.example.address.bean.ResultVO;
import com.example.address.service.IAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/addresses")
@Api(value = "地址操作接口",tags = "地址管理")
public class AddressController{
    @Autowired
    private IAddressService addressService;

    @ApiOperation("添加地址操作接口")
//    @ApiImplicitParams({
//            @ApiImplicitParam(dataType = "string",name = "username", value = "用户登录账号",required = true),
//            @ApiImplicitParam(dataType = "string",name = "password", value = "用户登录密码",required = true)
//    })
    @PostMapping ("/add_new_address")
    public ResultVO addNewAddress(@RequestBody Address form) {
        // 调用业务对象的方法执行业务
        ResultVO resultVO = addressService.addNewAddress(form);
        return resultVO;
    }

    @ApiOperation("查询全部地址操作接口")
    @GetMapping({"/list"})
    public ResultVO listAddrs() {
        ResultVO resultVO = addressService.listAddrs();
        return resultVO;
    }

    @ApiOperation("根据aid查询地址操作接口")
    @GetMapping({"/find"})
    public ResultVO findAddrsByaid(Integer aid) {
        ResultVO resultVO = addressService.getByAid(aid);
        return resultVO;
    }

    @ApiOperation("根据aid更新地址操作接口")
    @PutMapping({"/update"})
    public ResultVO findAddrsByaid(@RequestBody Address address) {
        ResultVO resultVO = addressService.updateByAid(address);
        return resultVO;
    }

    @ApiOperation("删除地址操作接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "aid", value = "地址id",required = true),
    })
    @DeleteMapping ("{aid}/delete")
    public ResultVO delete(@PathVariable("aid") Integer aid) {
        ResultVO resultVO = addressService.delete(aid);
        return resultVO;
    }

}

